﻿using Newtonsoft.Json;
using SetPriceCalculateV1d0.Entity;
using SetPriceCalculateV1d0.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetPriceCalculateV1d0
{
    /// <summary>
    /// 套餐最优价格计算帮助类
    /// </summary>
    public class SetOptimalPriceCalculateHelperV1d0
    {
        /// <summary>
        /// 选择最优套餐价格规则列表，并返回基于该列表的已点菜品价格信息列表json字符串
        /// </summary>
        /// <param name="itemSetPriceRuleListJson">套餐价格规则列表json字符串</param>
        /// <param name="itemPriceInfoListJson">已点菜品价格信息列表json字符串</param>
        /// <returns>最优菜品价格信息列表的json字符串</returns>
        public static string GetOptimalPriceInfoList(string itemSetPriceRuleListJson, string itemPriceInfoListJson)
        {
            //根据json字符串生成 ItemSetPriceRuleSetV1d0对象
            ItemSetPriceRuleSetV1d0 ruleSet = new ItemSetPriceRuleSetV1d0();
            ruleSet.ItemSetPriceRuleList = JsonConvert.DeserializeObject<List<ItemSetPriceRuleV1d0>>(itemSetPriceRuleListJson) as List<ItemSetPriceRuleV1d0>;

            //根据json字符串生成 ItemPriceInfoSetV1d0对象
            ItemPriceInfoSetV1d0 priceInfoSet = new ItemPriceInfoSetV1d0();
            priceInfoSet.ItemPriceInfoList=JsonConvert.DeserializeObject<List<ItemPriceInfoV1d0>>(itemPriceInfoListJson) as List<ItemPriceInfoV1d0>;
            //得到最优规则列表
            List<ItemSetPriceRuleV1d0> optimalRuleList = GetOptimalRuleList(ruleSet, priceInfoSet);
            //根据最优规则列表，更新已点菜品中的配菜价格信息
            int index = 0;
            for (int i=0;i< priceInfoSet.ItemPriceInfoList.Count;i++)
            {
                if (priceInfoSet.ItemPriceInfoList[i].ItemLevel >= 50)
                {
                    priceInfoSet.ItemPriceInfoList[i].ItemPrice = optimalRuleList[index].SubPrice;
                    index++;
                }
            }
            string priceInfoListString = JsonConvert.SerializeObject(priceInfoSet.ItemPriceInfoList);           
            return priceInfoListString;
        }
        /// <summary>
        /// 选择最优套餐价格规则列表，并返回基于该列表的已点菜品价格信息列表json字符串
        /// </summary>
        /// <param name="itemSetPriceRuleListJson">套餐价格规则列表json字符串</param>
        /// <param name="itemPriceInfoListJson">已点菜品价格信息列表json字符串</param>
        /// <returns>最优菜品价格信息列表的json字符串</returns>
        public string GetOptimalPriceInfoListInstance(string itemSetPriceRuleListJson, string itemPriceInfoListJson)
        {
            //根据json字符串生成 ItemSetPriceRuleSetV1d0对象
            ItemSetPriceRuleSetV1d0 ruleSet = new ItemSetPriceRuleSetV1d0();
            ruleSet.ItemSetPriceRuleList = JsonConvert.DeserializeObject<List<ItemSetPriceRuleV1d0>>(itemSetPriceRuleListJson) as List<ItemSetPriceRuleV1d0>;

            //根据json字符串生成 ItemPriceInfoSetV1d0对象
            ItemPriceInfoSetV1d0 priceInfoSet = new ItemPriceInfoSetV1d0();
            priceInfoSet.ItemPriceInfoList = JsonConvert.DeserializeObject<List<ItemPriceInfoV1d0>>(itemPriceInfoListJson) as List<ItemPriceInfoV1d0>;
            //得到最优规则列表
            List<ItemSetPriceRuleV1d0> optimalRuleList = GetOptimalRuleList(ruleSet, priceInfoSet);
            //根据最优规则列表，更新已点菜品中的配菜价格信息
            int index = 0;
            for (int i = 0; i < priceInfoSet.ItemPriceInfoList.Count; i++)
            {
                if (priceInfoSet.ItemPriceInfoList[i].ItemLevel >= 50)
                {
                    priceInfoSet.ItemPriceInfoList[i].ItemPrice = optimalRuleList[index].SubPrice;
                    index++;
                }
            }
            string priceInfoListString = JsonConvert.SerializeObject(priceInfoSet.ItemPriceInfoList);
            return priceInfoListString;
        }

        /// <summary>
        /// 得到最优价格套餐的规则列表
        /// </summary>
        /// <param name="itemSetPriceRuleSet"></param>
        /// <param name="itemOrderedPriceInfoSet"></param>
        /// <returns></returns>
        private static List<ItemSetPriceRuleV1d0> GetOptimalRuleList(ItemSetPriceRuleSetV1d0 itemSetPriceRuleSet, ItemPriceInfoSetV1d0 itemOrderedPriceInfoSet)
        {
            var ruleListList = HotchPotchV1d0.GetSingleItemValidRuleListList(itemSetPriceRuleSet, itemOrderedPriceInfoSet);
            //得到笛卡尔积
            List<List<ItemSetPriceRuleV1d0>> combinationRuleList = HotchPotchV1d0.Descartes<ItemSetPriceRuleV1d0>(ruleListList);
            float optimalSetPrice = 99999;
            //已点菜品列表的级别序列字符串
            string itemOrderedLevelSequence = ItemPriceInfoSetV1d0.GetStringOfLevelSequence(itemOrderedPriceInfoSet);
            List<ItemSetPriceRuleV1d0> optimalRuleList = new List<ItemSetPriceRuleV1d0>();
            foreach (var ruleList in combinationRuleList)
            {
                //规则列表的级别序列字符串
                string ruleListLevelSequence = ItemSetPriceRuleSetV1d0.GetStringOfLevelSequence(ruleList);
                //判断规则列表的级别序列字符串是否与已点菜品列表的级别序列字符串相同，如果相同，该规则列表有效，否则该规则无效
                bool isValid = ruleListLevelSequence.Equals(itemOrderedLevelSequence);
                if (isValid)
                {
                    float sum = 0;
                    ruleList.ForEach(x => sum += x.SubPrice);
                    if (sum < optimalSetPrice)
                    {
                        //最优总价
                        optimalSetPrice = sum;
                        //最优规则列表
                        optimalRuleList = ruleList;
                    }
                }
            }
            return optimalRuleList;
        }
    }
}
